package cn.aezo.minions.web.services.impl;

import cn.aezo.core.entity.datasource.ColumnDefinition;
import cn.aezo.core.entity.datasource.DBConfig;
import cn.aezo.core.entity.datasource.TableDefinition;
import cn.aezo.core.entity.datasource.TableInfo;
import cn.aezo.minions.gen.datasource.mysql.MySqlDataSourceInfo;
import cn.aezo.minions.gen.mapper.ModuleFunctionDao;
import cn.aezo.minions.gen.mapper.ProjectConfigDao;
import cn.aezo.minions.gen.mapper.ProjectModuleDao;
import cn.aezo.minions.gen.model.ModuleFunction;
import cn.aezo.minions.gen.model.ProjectConfig;
import cn.aezo.minions.gen.model.ProjectModule;
import cn.aezo.minions.web.services.ModuleFunctionService;
import cn.aezo.minions.utils.SqlHelper;
import cn.aezo.utils.base.MiscU;
import cn.aezo.utils.base.StringU;
import cn.aezo.utils.base.ValidU;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by smalle on 2017/10/28.
 */
@Service
public class ModuleFunctionServiceImpl implements ModuleFunctionService {
    @Autowired
    private ProjectConfigDao projectConfigDao;

    @Autowired
    private ProjectModuleDao projectModuleDao;

    @Autowired
    private ModuleFunctionDao moduleFunctionDao;

    @Override
    public Object initFunction(Long projectConfigId) {
        if(ValidU.isEmpty(projectConfigId)) return null;

        // 获取默认模块
        ProjectModule projectModule = projectModuleDao.getDefaultModule();
        if(ValidU.isEmpty(projectModule)) {
            projectModuleDao.insert(new ProjectModule(projectConfigId, ProjectModule.DefaultModuleName, "默认", ""));
            projectModule = projectModuleDao.getDefaultModule();
        }
        if(ValidU.isEmpty(projectModule)) return null;
        Long projectModuleId = projectModule.getProjectModuleId();

        // 获取数据库所有表信息
        ProjectConfig projectConfig = projectConfigDao.findOne(projectConfigId);
        DBConfig dbConfig = this.getDBConfig(projectConfig);
        SqlHelper sqlHelper = SqlHelper.getInstance(dbConfig);
        MySqlDataSourceInfo info = MySqlDataSourceInfo.getInstance(sqlHelper);

        TableInfo tableInfo = info.getTableInfo();
        List<TableDefinition> tables = tableInfo.getTableDefinitions(dbConfig.getDbName());
        if(ValidU.isEmpty(tables)) return null;

        // 初始化功能
        moduleFunctionDao.deleteByProjectModuleId(projectModuleId);

        for (TableDefinition table : tables) {
            String tableName = table.getTableName();
            // 主键
            ColumnDefinition columnDefinition = table.getPkColumn();
            String pkName = columnDefinition.getColumnName();

            String camelTableName = StringU.underline2Camel(tableName, true);
            String camelPkName = StringU.underline2Camel(pkName, true);

            // 增删查改
            List<String> actions = MiscU.Instance.toList("get", "find", "edit", "delete");
            for (String action : actions) {
                ModuleFunction moduleFunction = null;
                String functionName = action + StringU.toUpperCaseFirst(camelTableName);
                switch (action) {
                    case "get":
                        moduleFunction = new ModuleFunction(projectModuleId, functionName, functionName, camelTableName + "/{"+ camelPkName +"}", null, "Object");
                        break;
                    case "find":
                        moduleFunction = new ModuleFunction(projectModuleId, functionName, functionName, camelTableName, null, "Object");
                        break;
                    case "edit":
                        moduleFunction = new ModuleFunction(projectModuleId, functionName, functionName, camelTableName, "POST", "Object");
                        break;
                    case "delete":
                        moduleFunction = new ModuleFunction(projectModuleId, functionName, functionName, camelTableName + "/{"+ camelPkName +"}", "POST", "Object");
                        break;
                    default:
                        break;
                }

                if(moduleFunction != null)
                    moduleFunctionDao.insert(moduleFunction);
            }
        }

        return null;
    }

    /**
     * 连接数据库
     * @param projectConfig
     * @return
     */
    public DBConfig getDBConfig(ProjectConfig projectConfig) {

        String ip = projectConfig.getDatabaseIp();
        String port = projectConfig.getDatabasePort();
        String username = projectConfig.getDatabaseUsername();
        String password = projectConfig.getDatabasePassword();
        String driver = projectConfig.getDatabaseDriver();
        String dbName = projectConfig.getDatabaseDbName();

        DBConfig dbConfig = new DBConfig(ip, Integer.parseInt(port), username, password, driver, dbName);
        return dbConfig;
    }
}
